package com.xiyu.service.service.unit;

import com.xiyu.service.vo.unit.unit.UnitGetGroupCategoryInfoOutput;
import com.xiyu.service.vo.unit.unit.UnitSimpleListOutput;
import com.xiyu.service.vo.unit.unit.UnitSingleGetOutput;
import com.xiyu.service.vo.unit.unit.UnitUpdateInput;
import com.xiyu.service.vo.unit.unit.UnitCreateInput;
import com.xiyu.service.model.unit.unit.UnitUnit;
import com.xiyu.service.repository.unit.unit.UnitUnitRepository;
import com.xiyu.service.vo.unit.unit.UnitPageQueryOutput;
import com.xiyu.service.vo.unit.unit.UnitPageQueryInput;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.babyfish.jimmer.Page;
import java.util.*;

import org.springframework.transaction.annotation.Transactional;
import com.xiyu.service.util.entity.EntityUtils;
import com.xiyu.service.framework.web.web.core.pojo.PageResult;
import com.xiyu.service.convert.unit.UnitConvert;

import static com.xiyu.service.framework.exception.util.ServiceExceptionUtil.exception;
import static com.xiyu.service.errorCode.unit.UnitErrorCode.*;

/**
 * 单位管理 Service 实现类
 */
@Service
@Validated
public class UnitServiceImpl implements UnitService {

    @Resource
    private UnitUnitRepository unitUnitRepository;




    @Override
    public PageResult<UnitPageQueryOutput> pageQuery(UnitPageQueryInput inputVO) {
        Page<UnitUnit> pageOfficeUnit = unitUnitRepository.pageQuery(inputVO);
        List<UnitPageQueryOutput> listOfficeUnit = UnitConvert.INSTANCE.pageQueryOutputConvert(pageOfficeUnit.getRows());
        return new PageResult<>(listOfficeUnit, pageOfficeUnit.getTotalRowCount());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(UnitCreateInput inputVO) {
        UnitUnit newOfficeUnit = UnitConvert.INSTANCE.createInputConvert(inputVO);
        newOfficeUnit = unitUnitRepository.insert(newOfficeUnit);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(UnitUpdateInput inputVO) {
        Optional<UnitUnit> optionalOldOfficeUnit = unitUnitRepository.findById(inputVO.getId());
        if(!optionalOldOfficeUnit.isPresent()){
            throw exception(OFFICE_UNIT_NOT_EXIST);
        }
        UnitUnit updateOfficeUnit = UnitConvert.INSTANCE.updateInputConvert(inputVO);
        if (!EntityUtils.isEquals(optionalOldOfficeUnit.get(), updateOfficeUnit))
            unitUnitRepository.update(updateOfficeUnit);
        return true;
    }

    @Override
    public UnitSingleGetOutput singleGet(Long id) {
        Optional<UnitUnit> optionOfficeUnit = unitUnitRepository.findById(id);
        if(!optionOfficeUnit.isPresent()){
            throw exception(OFFICE_UNIT_NOT_EXIST);
        }
        return UnitConvert.INSTANCE.singleGetOutputConvert(optionOfficeUnit.get());

    }

    @Override
    public List<UnitSimpleListOutput> simpleList() {
        return UnitConvert.INSTANCE.simpleListOutputConvert(unitUnitRepository.findAll());
    }

    @Override
    public List<UnitGetGroupCategoryInfoOutput> getGroupCategoryInfo() {
        return UnitConvert.INSTANCE.getGroupCategoryInfoOutputConvent(unitUnitRepository.findGroupCategory());
    }

}
